/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef CELLS_H
#define CELLS_H
#include <QColor>
#include <QGraphicsItem>
#include <QPainter>
#include "common_struct/common_struct.h"

#define ISNEAR 1

#if  1
#define AIRCRAFT_SIZE 12
#define LOCATION_ICON_SIZE 15
#define IMG_SIZE 1024    //32或64，可以改10km   256--2km
#define TRACKER_WIDTH 1.5
#else
#define AIRCRAFT_SIZE 30
#define LOCATION_ICON_SIZE 30
#define IMG_SIZE 256    //32或64，可以改10km   256--2km
#define TRACKER_WIDTH 2
#endif

//!maptile
class MapTile : public QGraphicsItem
{
public:
    explicit MapTile(int tile_x, int tile_y, int zoom, QGraphicsItem* parent = 0,MapType maptype=StreetMap, uint32_t tile_width = IMG_SIZE, uint32_t tile_height = IMG_SIZE);
    ~MapTile();

    QRectF boundingRect() const Q_DECL_OVERRIDE;
    QPainterPath shape() const Q_DECL_OVERRIDE;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) Q_DECL_OVERRIDE;
    enum {Type = UserType + 1 };
    int type() const {return Type;}

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;

private:
    int m_tile_x;
    int m_tile_y;
    int m_zoom;
    MapType m_MapType;
    uint32_t m_tile_size_width;
    uint32_t m_tile_size_height;
    QImage* img;
};


//!aircraft icon
class Aircraft : public QGraphicsItem
{
public:
    Aircraft(uint32_t TargetID, AircraftParam& TargetInfo, uint8_t TargetMark=0);
    Aircraft(uint32_t TargetID=0, float distance=0, float angle=0, float speed=0, uint8_t TargetMark=0);
    ~Aircraft();

    QRectF boundingRect() const Q_DECL_OVERRIDE;
    QPainterPath shape() const Q_DECL_OVERRIDE;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) Q_DECL_OVERRIDE;

    static float Aircraft_size_R;
    static float GoldenRatio;
    enum {Type = UserType + 2};
    int type() const{return Type;}
    void setColor(QColor color);
    void setPositionInfo(AircraftParam &params);
protected:

    void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;

private:
    //target params
    uint32_t m_TargetID;
    AircraftParam m_AircraftParam;
    uint8_t m_TargetMark;

    //paint params
    float m_R;
    float m_r;

    //display params
    bool ispressd;
    static int track_id_max;
    QString info;
    QColor m_color;
    QGraphicsTextItem *label;
    QPainterPath createPaintPath(float R, float r);
    QString creatTargetInfoText(uint8_t TargetInfo);

#if IMAGE_USING
    QImage* img;
#endif
};


//! Location of radar
class LocationIcon : public QGraphicsItem
{
public:
    explicit LocationIcon(RadarParamInfoStruct info);
    explicit LocationIcon(float RadarDetectionDistance, float DirectionAngle, float StartAngle, float EndAngle);
    ~LocationIcon();
    QRectF boundingRect() const Q_DECL_OVERRIDE;
    QPainterPath shape() const Q_DECL_OVERRIDE;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) Q_DECL_OVERRIDE;
    enum {Type = UserType + 3 };
    int type() const{return Type;}

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;

private:
    float m_RadarDetectionDistance;
    float m_DirectionAngle;
    float m_StartAngle;
    float m_EndAngle;
    RadarParamInfoStruct m_radarinfo;
    QVector<QPointF> stuff;
    QImage img;
};


//! location png
class LocationPng : public QGraphicsItem
{
public:
    explicit LocationPng();
    ~LocationPng();
    QRectF boundingRect() const Q_DECL_OVERRIDE;
    QPainterPath shape() const Q_DECL_OVERRIDE;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) Q_DECL_OVERRIDE;
    enum { Type = UserType + 4 };
    int type() const{return Type;}
private:
    QImage img;
};


//!MonitoringAreaItem
class MonitoringAreaItem : public QGraphicsItem
{
public:
    MonitoringAreaItem(float RadarDetectionDistance, std::vector<dvec2> *path);
    ~MonitoringAreaItem();
    QRectF boundingRect() const Q_DECL_OVERRIDE;
    QPainterPath shape() const Q_DECL_OVERRIDE;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) Q_DECL_OVERRIDE;
    enum {Type = UserType + 5 };
    int type() const{return Type;}

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;

private:
    std::vector<dvec2> m_path;
    float m_RadarDetectionDistance;
};

//!Track
class Tracker: public QGraphicsItem
{
public:
    explicit Tracker(const uint32_t TargetID, QPointF startPos, QColor color = Qt::black);
    ~Tracker();
    QRectF boundingRect() const Q_DECL_OVERRIDE;
    QPainterPath shape() const Q_DECL_OVERRIDE;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) Q_DECL_OVERRIDE;
    void Lineto(QPointF pos);
    void setTrackerColor(QColor& color);

    void setAreaType(AreaType type);
    bool isColored();
    QColor& TrackerColor();
    enum {Type = UserType + 2000 };
    int type() const{return Type;}

    Aircraft* icon;
    QVector<QPointF> Tracker_vec;
    QVector<QGraphicsLineItem*> Lines;
    QPointF m_Region;
private:
    int counter;
    uint32_t m_TrackID;
    QColor m_color;
    QPointF m_startPos;
    QPointF m_endpos;
    AreaType DisplayType;
    QColor MonitorAreaPen;
    QColor MonitorAreaBrush;
    QColor AlertAreaPen;
    QColor AlertAreaBrush;
    QColor defaultColor;
    bool m_isColored;
};


class PolygonAreaItem :public QGraphicsItem
{
public:
    explicit PolygonAreaItem(const uint32_t AreaId, QPointF startPos, AreaType type = MonitorArea);
    ~PolygonAreaItem();
    QRectF boundingRect() const Q_DECL_OVERRIDE;
    QPainterPath shape() const Q_DECL_OVERRIDE;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) Q_DECL_OVERRIDE;
    void Lineto(QPointF pos);
    enum {Type = UserType+10};
    int type() const{return Type;}

public slots:
    void PolygonFinished();

private:
    uint32_t m_AreaId;
    AreaType m_Areatype;
    QPointF  m_startPos;
    QPointF  m_endPos;
    QPointF  m_Region;
    QVector<QPointF> PolygonData;
    bool isFinished;
    QColor MonitorAreaPen;
    QColor MonitorAreaBrush;
    QColor AlertAreaPen;
    QColor AlertAreaBrush;
};


#endif // MapTile_H
